package cn.autumnorange.authorization.security.service;

import cn.autumnorange.authorization.security.controller.AdminRepository;
import cn.merryyou.dto.system.userDetails.UserDetailsImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;

@Transactional(rollbackFor = Exception.class, readOnly = true, propagation = Propagation.REQUIRED)
@Service(value = "userDetailsServiceImpl")
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    AdminRepository adminRepository;

    @Transactional(readOnly = true)
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        //        SysUserEntity sysUser = sysUserRepository.getOneByCode(s);
        //        System.out.println("sysUser++++++++++++++++++++++++++++++++++++++++：" + sysUser);
        //        List<SysRoleUserEntity> sysRoleUserList =
        // sysRoleUserRepository.findAllByUserId(sysUser.getId());
        //        System.out.println("sysRoleUserList：" + sysRoleUserList);
        //        List<String> roleIdList = new ArrayList();
        //        for (SysRoleUserEntity sysRoleUserEntity : sysRoleUserList) {
        //            roleIdList.add(sysRoleUserEntity.getRoleId());
        //        }
        //        List<SysRoleEntity> sysRoleEntities = sysRoleRepository.findAllById(roleIdList);
        //        System.out.println("sysRoleEntities：" + sysRoleEntities);
        //        sysUser.setRoles(sysRoleEntities);
        //        return sysUser;
        //        Json json = schedualServiceHi.testJson(s);
        //        System.out.println("json feign value:" + json.getName());
        cn.autumnorange.authorization.security.controller.UserDetailDto userDetails =
                adminRepository.findAdminEntityByUserName(s).createUserDetailDto();

        System.out.println("     suserDetails   " + userDetails);
//        Collection<GramtedAuthorityImpl> gramtedAuthorities = userDetails.getAuthorities();
        Collection<SimpleGrantedAuthority> collection = new ArrayList();
//        for (GramtedAuthorityImpl gramtedAuthority : gramtedAuthorities) {
//            collection.add(new SimpleGrantedAuthority(gramtedAuthority.getAuthority()));
//        }
        collection.add(new SimpleGrantedAuthority("ROLE_admin"));
        UserDetailsImpl userDetails1 =
                new UserDetailsImpl(
                        null,
                        userDetails.getUsername(),
                        userDetails.getPassword(),
                        collection,
                        true,
                        true,
                        true,
                        true);
        return userDetails1;
    }
}
